/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package loaders;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author Franco
 */
public class XMLoader {
    
    private ArrayList<String> sqlToInserts = new ArrayList<String>();
    private String filePath;
    private String[] columns = {""};
    
    
    public XMLoader(String fileToLoad){
        this.filePath = fileToLoad;
    }
    
    public void doMagic() throws Exception{
        File fXmlFile = new File("C:\\Users\\Franco\\Documents\\Facu\\Sistemas de Gestion II\\TP2\\Customer_R.xml");        
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);
        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
        NodeList nList = doc.getElementsByTagName("cliente");
        System.out.println("-----------------------");

        
        
        for (int temp = 0; temp < nList.getLength(); temp++) {
            String sql = "INSERT INTO Customers VALUES (";
            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {                
                Element eElement = (Element) nNode;
                StringBuilder string = new StringBuilder();
                String cid = getTagValue("CUSTOMER_ID", eElement).trim();
                string.append("'").append(cid).append("'").append(",");
                string.append(getTagValue("FULL_NAME", eElement)).append(",");
                
                string.append(getTagValue("BIRTH_DATE", eElement)).append(",");
                string.append(getTagValue("CITY", eElement)).append(",");
                string.append(getTagValue("STATE", eElement)).append(",");
                string.append(getTagValue("ZIPCODE", eElement)).append(")");                                                
                sql += string;
            }
            sqlToInserts.add(sql);
        }
        System.out.println(sqlToInserts);
    }
        
    
    
    private static String getTagValue(String sTag, Element eElement) {
	NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
 
        Node nValue = (Node) nlList.item(0);
 
	return nValue.getNodeValue();
    }
    
}
